package com.gitee.mars.vaccin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.vaccin.entity.SysUser;
import com.gitee.mars.vaccin.exception.WorkException;
import com.gitee.mars.vaccin.request.DevelopPlaceReq;
import com.gitee.mars.vaccin.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.vaccin.service.IDevelopPlaceService;
import com.gitee.mars.vaccin.mapper.DevelopPlaceMapper;
import com.gitee.mars.vaccin.entity.DevelopPlace;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Objects;


/**
 * DevelopPlace 实现层
 *
 * @Author wq
 * @Date 2021-04-11
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class DevelopPlaceServiceImpl extends ServiceImpl<DevelopPlaceMapper, DevelopPlace> implements IDevelopPlaceService {


    @Override
    public void add(String address) {
        if (StringUtils.isEmpty(address)) {
            throw new WorkException("请输入研发地址");
        }
        DevelopPlace developPlace = new DevelopPlace();
        developPlace.setAddress(address);
        developPlace.setCreateTime(new Date());
        developPlace.setUpdateTime(new Date());
        save(developPlace);
    }

    @Override
    public PageInfo<DevelopPlace> list(DevelopPlaceReq request) {
        IPage<DevelopPlace> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<DevelopPlace> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(request.getAddress())) {
            wrapper.like(DevelopPlace::getAddress, request.getAddress());
        }
        IPage<DevelopPlace> developPlaceIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(developPlaceIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), developPlaceIPage.getRecords());
        }
        return null;
    }
}